var utils = require('../../../utils/util.js');
var c = require('../../../utils/config.js');
Page({
    data: {
        bank_id:0,
        bank_list:[],
        is_auto_show:false,
        checked:false
    },
    onLoad: function () {
        utils.checkLogin();
        if (wx.getStorageSync("userinfo").is_auto) {
          this.setData({
              is_auto_show:true
          });
        }
        this.getDefaultBank();
    },
    getDefaultBank:function(){
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
            url:"/wsgoods/userbank/getDefault",
            token:token,
        };
        utils.request(data).then(function(res){
            if (res.code == 0) {
                var data = res.data;
                data['img_info']['pic'] = c.img_url+data['img_info']['pic'];
                data['bank_no'] = data['bank_no'].substr(data['bank_no'].length-4);
                that.setData({
                    bank_list:data
                });
            }
        });
    },
    submit:function(){
        var that = this;
        var token = wx.getStorageSync("userinfo").wap_token;
        var is_auto = this.data.is_auto;
        var bank_id = this.data.bank_list.id;
        var checked = this.data.checked;
        if (!checked) {
          wx.showToast({
              title: '请阅读并同意《自动提现服务协议》',
              icon: 'none',
              duration: 1500
          });
          return false;
        }
        if (is_auto && !bank_id) {
          wx.showToast({
              title: '银行卡错误！',
              icon: 'none',
              duration: 1500
          });
          return false;
        }
        var data = {
            url:"/yluser/withdraw/add",
            token:token,
            is_auto:is_auto,
            bank_id:bank_id
        };
        utils.request(data).then(function(res){
            if (res.code == 0) {
                wx.showToast({
                    title: '申请成功',
                    icon: 'none',
                    duration: 1500
                });
                setTimeout(function(){
                    wx.navigateBack({
                      delta: 1,
                    })
                }, 1000);
            }
        });
    },
     switchChange: function (e){
      if (e.detail.value==true) {
          this.setData({
              is_auto:1,
          })
      }else{
        this.setData({
              is_auto:0,
          })
      }
    },
    checkboxChange:function(e){
        this.setData({
            checked:!this.data.checked,
        })
    }
})